package org.fujay.langchain4jserver.domain.conversation.entity;

import lombok.*;
import org.fujay.langchain4jserver.domain.application.vo.AppId;
import org.fujay.langchain4jserver.domain.conversation.vo.ConversationId;
import org.fujay.langchain4jserver.domain.conversation.vo.UserId;

import java.time.LocalDateTime;

/**
 * 会话实体，聚合根的一部分
 */
@Getter
@Setter(AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Conversation {

    @NonNull
    private ConversationId id;

    @NonNull
    private UserId userId;

    @NonNull
    private AppId appId;

    private String title;

    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    // 工厂方法: 创建新会话
    public static Conversation create(ConversationId id, UserId userId, AppId appId) {
        return new Conversation(
                id,
                userId,
                appId,
                "New Conversation", // 默认标题
                LocalDateTime.now(),
                LocalDateTime.now());
    }

    // 工厂方法: 加载现有会话
    public static Conversation load(
            ConversationId id,
            UserId userId,
            AppId appId,
            String title,
            LocalDateTime createTime,
            LocalDateTime updateTime) {
        return new Conversation(id, userId, appId, title, createTime, updateTime);
    }

    // 更新标题
    public void updateTitle(String newTitle) {
        this.title = newTitle;
        this.updateTime = LocalDateTime.now();
    }

    // 供聚合根更新时间戳
    public void touch() {
        this.updateTime = LocalDateTime.now();
    }
}
